package com.hnucm.cooklife.presenter.me;

import androidx.annotation.NonNull;

import com.hnucm.cooklife.base.BasePresenter;
import com.hnucm.cooklife.bean.db.Dish;
import com.hnucm.cooklife.bean.me.DishColl;
import com.hnucm.cooklife.bean.me.ToAddCollectData;
import com.hnucm.cooklife.contract.mecollect.Contract;
import com.hnucm.cooklife.model.MeCollectModel;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MeCollectPresenter extends BasePresenter<Contract.IMeCollectView> implements Contract.IMeCollectPresenter {
    Contract.IMeCollectModel mIMeCollectModel;

    public MeCollectPresenter() {
        mIMeCollectModel = new MeCollectModel();
    }

    @Override
    public void loadMeCollect(int userId) {

        mIMeCollectModel.loadMeCollect(userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<DishColl>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(@NonNull List<DishColl> dishList) {
                        if(isViewAttached()){
                            getView().onLoadMeCollect(dishList);
//                            System.out.println(dishList);
//                            System.out.println("pre");
                            getView().onLoadSuccess();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        e.printStackTrace();
                        System.out.println(e.getMessage()+"sadly");
                        if(isViewAttached()){
                            getView().onLoadingFailed();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void refreshMeCollect(int userId) {

    }

    @Override
    public void unCollect(int userId, int dishId) {
        mIMeCollectModel.unCollect(userId, dishId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ToAddCollectData>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(@NonNull ToAddCollectData toAddCollectData) {
                        if(isViewAttached()){
                            getView().onUnCollect(toAddCollectData,dishId);
                            getView().onLoadSuccess();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        e.printStackTrace();
                        System.out.println(e.getMessage());
                        if(isViewAttached()){
                            getView().onLoadingFailed();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
